import { ProTableProps, ProColumnType } from '@ant-design/pro-table';

declare type ColumnType = ProColumnType;
declare type ColumnMapType = {
  [key: string]: ColumnType;
}
declare type ColumnsType = ProTableProps<any, any>['columns'];

class ColumnFactory {

  private readonly props: ColumnType;

  constructor(props?: ColumnType) {
    this.props = props || {};
  }

  private wrapper(props?: ColumnMapType): ColumnMapType {
    return props ? props : {};
  }

  public getIdEntityColumns(props?: ColumnMapType): ColumnsType {
    return [
      { dataIndex: 'id', title: '主键', ... this.props, ... this.wrapper(props)['id'] },
    ];
  }

  public getAbstractBaseEntityColumns(props?: ColumnMapType): ColumnsType {
    return [
      { dataIndex: 'tenantId', title: '租户Id', ... this.props, ... this.wrapper(props)['tenantId'] },
      { dataIndex: 'createTime', title: '创建时间', ... this.props, ... this.wrapper(props)['createTime'] },
      { dataIndex: 'updateTime', title: '修改时间', ... this.props, ... this.wrapper(props)['updateTime'] },
      { dataIndex: 'createUser', title: '创建者', ... this.props, ... this.wrapper(props)['createUser'] },
      { dataIndex: 'updateUser', title: '修改者', ... this.props, ... this.wrapper(props)['updateUser'] },
      { dataIndex: 'version', title: '版本号', ... this.props, ... this.wrapper(props)['version'] },
    ];
  }

  public getAbstractIdBaseEntityColumns(props?: ColumnMapType): ColumnsType {
    return [ ... this.getIdEntityColumns(props) as any, ... this.getAbstractBaseEntityColumns(props) as any ];
  }

  public wrapperProColumns(columns?: ColumnsType): ColumnsType {
    const list: ColumnsType = [];

    columns?.forEach(i => {
      const column: ColumnType = { ... i };
      const { valueType = 'text', title } = column;
      if (valueType === 'text' || valueType === 'textarea') {
        column['fieldProps'] = {
          ... i?.fieldProps,
          placeholder: '请输入' + title,
        };
      } else if (valueType === 'select') {
        column['fieldProps'] = {
          ... i?.fieldProps,
          placeholder: '请选择' + title,
        };
      }
      list.push(column);
    });

    return list;
  }
}

const factory = new ColumnFactory({
  hidden: true,
  hideInSetting: true,
  hideInForm: true,
  hideInTable: true,
  hideInDescriptions: true,
  hideInSearch: true,
});

export {
  factory, ColumnFactory,
};